<template>
  <el-dialog
    :visible.sync="isShow"
    :title="{ add: '新增权限', edit: '编辑权限' }[mode]"
    @close="closeEvent"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="权限描述" prop="description">
        <el-input v-model="form.description" />
      </el-form-item>
      <el-form-item label="企业可见" prop="enVisible">
        <el-switch
          v-model="form.enVisible"
          active-text="可见"
          inactive-text="不可见"
          active-value="1"
          inactive-value="0"
          active-color="#13ce66"
          inactive-color="#ff4949"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <div style="text-align: center">
        <el-button @click="isShow = false">取消</el-button>
        <el-button type="primary" @click="submit">确定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script>
import { sysPermissionPost, sysPermissionPut } from '@/api/permission'
export default {
  data() {
    return {
      mode: 'add', // 区分新增和编辑add 与 edit
      isShow: false,
      form: {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '1', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      },
      rules: {
        name: [{ required: true, message: '必填', trigger: 'blur' }], //	string	非必须
        code: [{ required: true, message: '必填', trigger: 'blur' }], //	string	非必须
        description: [{ required: true, message: '必填', trigger: 'blur' }],
        enVisible: [{ required: true, message: '必填', trigger: 'blur' }] //	string	非必须
      }
    }
  },
  methods: {
    submit() {
      this.$refs.form.validate(async(val) => {
        if (val) {
          // console.log(this.form)
          if (this.mode === 'add') {
            // 如果点击新增按钮
            await sysPermissionPost(this.form)
            this.$message.success('添加成功')
          } else if (this.mode === 'edit') {
            // 如果点击编辑按钮
            await sysPermissionPut(this.form)
            this.$message.success('编辑成功')
          }
          this.isShow = false
          this.$emit('getData')
        }
      })
    },
    closeEvent() {
      this.$refs.form.resetFields()
      this.form = {
        name: '', //	string	非必须
        code: '', //	string	非必须
        description: '', //	string	非必须
        enVisible: '1', //	string	非必须
        type: '', //	number	非必须
        pid: '' //	string	非必须
      }
    }
  }
}
</script>

<style></style>
